﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Dapper;
using DapperExtensions;
using ExaminationSys.DataAccess.Entity;
using SQLinq;
using SQLinq.Dapper;

namespace ExaminationSys.DataAccess.Repository
{
    public class PaperDetailAutoRepository : Repository<PaperDetailAuto>
    {
        public PaperDetailAutoRepository(SqlConnection context)
            : base(context)
        {
            db = context;
        }
        private SqlConnection db;
        public override PaperDetailAuto GetById(dynamic id)
        {
            return base.ObjectSet.Get<PaperDetailAuto>(db, id, null, null);
        }
    }
    public class PaperDetailAutoService
    {
        private PaperDetailAutoService()
        {
        }

        public PaperDetailAuto Get(Guid id)
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                var result = work.PaperDetailAuto.GetById(id);
                return result;

            }
        }

        public bool CheckPaperID(string paperid, Guid id)
        {
            using (var db = DataContent.DBServer())
            {
                var pg = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<IPredicate>() };
                pg.Predicates.Add(Predicates.Field<PaperDetailAuto>(f => f.PaperID, Operator.Eq, paperid));
                pg.Predicates.Add(Predicates.Field<PaperDetailAuto>(f => f.ID, Operator.Eq, id, true));
                IUnitOfWork work = new UnitOfWork(db);
                var result = work.PaperDetailAuto.GetList(pg).FirstOrDefault();
                return result != null;
            }
        }

        public List<PaperDetailAuto> GetPaperDetailAuto(string paperid)
        {
            using (var db = DataContent.DBServer())
            {
                var pg = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<IPredicate>() };
                pg.Predicates.Add(Predicates.Field<PaperDetailAuto>(f => f.PaperID, Operator.Eq, paperid));
                IUnitOfWork work = new UnitOfWork(db);
                var result = work.PaperDetailAuto.GetList(pg);
                return result.ToList();
            }
        }

        public List<PaperDetailAuto> GetAll()
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                var result = work.PaperDetailAuto.GetList();
                return result.ToList();
            }
        }

        public bool Delete(List<string> ids)
        {
            if (ids == null || ids.Count == 0) return false;
            bool flag = false;
            using (var db = DataContent.DBServer())
            {
                int retCode = 0;
                foreach (var id in ids)
                {
                    retCode = db.Execute(@"update PaperDetailAuto set Status=0 where ID=@TmpId",
                                         new[] { new { TmpId = id } });
                    if (retCode <= 0)
                    {
                        break;
                    }
                }
                flag = true;
            }
            return flag;
        }

        public bool Lock(List<string> ids, int _lock)
        {
            if (ids == null || ids.Count == 0) return false;
            bool flag = false;
            using (var db = DataContent.DBServer())
            {
                int retCode = 0;
                foreach (var id in ids)
                {
                    retCode = db.Execute(@"update PaperDetailAuto set Lock=@lk where ID=@TmpId",
                                         new[] { new { lk = _lock, TmpId = id } });
                    if (retCode <= 0)
                    {
                        break;
                    }
                }
                flag = true;
            }
            return flag;
        }

        public bool Update(PaperDetailAuto entity)
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                return work.PaperDetailAuto.Update(entity);
            }
        }
        public dynamic Insert(PaperDetailAuto entity)
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                return work.PaperDetailAuto.Insert(entity, null);
            }
        }

        public List<PaperDetailAuto> GetListByPage(string sqlwhere, string orderbyFiled, int pagesize, int pageindex, out int totalPage, out int totalRecord)
        {
            if (string.IsNullOrWhiteSpace(orderbyFiled))
                throw new ArgumentNullException("orderbyFiled");

            if (string.IsNullOrWhiteSpace(sqlwhere))
                sqlwhere = " 1=1";
            using (var db = DataContent.DBServer())
            {
                var obj = new
                {
                    TableName = "PaperDetailAuto",
                    Fields = "*",
                    OrderField = orderbyFiled,
                    sqlWhere = sqlwhere,
                    pageSize = pagesize,
                    pageIndex = pageindex,
                };

                var dynamicParam = new DynamicParameters(obj);
                dynamicParam.Add("TotalPage", null, DbType.Int32, ParameterDirection.Output, 32);
                dynamicParam.Add("TotalRecord", 0, DbType.Int32, ParameterDirection.Output, 32);

                var items = db.Query<PaperDetailAuto>("PagingRecord", dynamicParam, null, true, null, CommandType.StoredProcedure);
                totalPage = dynamicParam.Get<int>("TotalPage");
                totalRecord = dynamicParam.Get<int>("TotalRecord");
                return items.ToList();
            }
        }
    }
}
